package com.xjrsoft.module.customerTwo.AppManage.monthkpi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.dto.DwMonthKpiDataDto;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.dto.SaveDwMonthKpiDataFormDataDto;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.vo.DwMonthKpiDataVo;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.vo.DwMonthKpiDataListVo;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.dto.DwMonthKpiDataListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.excel.entity.XjrExcelImport;
import com.xjrsoft.module.excel.factory.ParserFactory;
import com.xjrsoft.module.excel.parser.ImportDataParser;
import com.xjrsoft.module.excel.service.IXjrExcelImportService;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.time.LocalDateTime;
import com.xjrsoft.common.serializers.LocalDateTimeDeserializer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.entity.DwMonthKpiData;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.vo.DwMonthKpiDataVo;
import com.xjrsoft.module.customerTwo.AppManage.monthkpi.service.IDwMonthKpiDataService;

/**
 *  控制器
 *
 * @author lisen
 * @since 2024-05-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/monthkpi")
@Api(value = "月度指标", tags = "月度指标接口")
public class DwMonthKpiDataController {

	private final IXjrExcelImportService importService;


	private final IDwMonthKpiDataService dwMonthKpiDataService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入dwMonthKpiData")
	@SaCheckPermission(value = {"monthkpi:view", "monthkpi:modify"}, mode = SaMode.OR)
	public Response<DwMonthKpiDataVo> getFormData(@PathVariable String id) {
		// 主表数据
		DwMonthKpiData dwMonthKpiData = dwMonthKpiDataService.getById(id);
		DwMonthKpiDataVo dwMonthKpiDataVo = BeanUtil.copy(dwMonthKpiData, DwMonthKpiDataVo.class);
		return Response.ok(dwMonthKpiDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入dwMonthKpiData")
		public Response<PageOutput<DwMonthKpiDataListVo>> getPageList(DwMonthKpiDataListDto listDto) {
		IPage<DwMonthKpiData> page = dwMonthKpiDataService.getPageList(listDto);
		List<DwMonthKpiDataListVo> records = BeanUtil.copyList(page.getRecords(), DwMonthKpiDataListVo.class);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入dwMonthKpiData")
	@SaCheckPermission("monthkpi:add")
	public Response save(@RequestBody SaveDwMonthKpiDataFormDataDto formDto) {
		DwMonthKpiDataDto dwMonthKpiDataDto = formDto.getDwMonthKpiDataDto();
		DwMonthKpiData dwMonthKpiData = BeanUtil.copy(dwMonthKpiDataDto, DwMonthKpiData.class);
		Integer dataId = dwMonthKpiDataService.addDwMonthKpiData(dwMonthKpiData);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{iid}")
	@ApiOperation(value = "修改", notes = "传入dwMonthKpiData")
	@SaCheckPermission("monthkpi:modify")
	public Response update(@PathVariable String iid, @RequestBody SaveDwMonthKpiDataFormDataDto formDto) {
		Integer id = Integer.parseInt(iid);
		DwMonthKpiDataDto dwMonthKpiDataDto = formDto.getDwMonthKpiDataDto();
		DwMonthKpiData dwMonthKpiData = BeanUtil.copy(dwMonthKpiDataDto, DwMonthKpiData.class);
		return Response.status(dwMonthKpiDataService.updateDwMonthKpiData(id, dwMonthKpiData));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"monthkpi:batchDelete", "monthkpi:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(dwMonthKpiDataService.delete(ids));
	}


	/**
	 * excel批量导入数据
	 */
	@PostMapping("/import")
	@ApiOperation(value = "excel批量导入数据", notes = "传入菜单id")
	@SaCheckPermission("monthkpi:import")
	public Response importData(@RequestParam("F_ModuleId") String moduleId, MultipartFile file) {
		XjrExcelImport excelImport = importService.getByModuleId(moduleId);
		ImportDataParser parser = ParserFactory.getImportDataParser();
		File excelFile = IoUtil.toFile(file);
		List<Map<String, Object>> recordList = parser.parseDataToMap(excelImport, excelFile);
		if (CollectionUtil.isNotEmpty(recordList)) {
			List<DwMonthKpiDataDto> entityDtoList = recordList.stream().map(record ->
				BeanUtil.toDtoBean(record, DwMonthKpiDataDto.class)
			).collect(Collectors.toList());
			List<DwMonthKpiData> entities = BeanUtil.copyList(entityDtoList, DwMonthKpiData.class);
			return Response.status(dwMonthKpiDataService.saveBatch(entities));
		}
		return Response.notOk();
	}
}
